<?php
namespace app\banquet\controller;
use app\AdminRest;


use app\banquet\model\Config;
use app\banquet\model\Evaluate;
use app\banquet\model\Order;
use app\banquet\model\Package;
use app\banquet\model\Place;
use app\banquet\model\PlaceDate;
use app\card\model\Company;
use app\card\model\User;
use think\App;
use think\facade\Db;


class AdminPlace extends AdminRest
{

    protected $model;

    protected $date_model;

    protected $package_model;



    public function __construct(App $app) {

        parent::__construct($app);

        $this->model  = new Place();

        $this->date_model  = new PlaceDate();

        $this->package_model  = new Package();


    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-22 17:47
     * @功能说明:套餐列表
     */
    public function packageList(){

        $input = $this->_param;

        $dis[] = ['a.status','>',-1];

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        if(!empty($input['title'])){

            $dis[] = ['a.title','like','%'.$input['title'].'%'];
        }

        if(!empty($input['company_id'])){

            $dis[] = ['c.company_id','=',$input['company_id']];

            $dis[] = ['c.status','>',-1];
        }

        $data = $this->package_model->adminList($dis,$input['limit']);

        return $this->success($data);


    }




    /**
     * @author chenniang
     * @DataTime: 2021-11-18 17:51
     * @功能说明:套餐下拉框
     */
    public function packageSelect(){

        $input = $this->_param;

        $dis[] = ['status','=',1];

        $dis[] = ['uniacid','=',$this->_uniacid];

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];
        }

        $data = $this->package_model->where($dis)->order('top desc,id desc')->select()->toArray();

        return $this->success($data);

    }



    /**
     * @author chenniang
     * @DataTime: 2021-11-18 17:51
     * @功能说明:添加套餐
     */
    public function packageAdd(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        $data = $this->package_model->dataAdd($input);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-11-18 17:51
     * @功能说明:编辑套餐
     */
    public function packageUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $input['uniacid'] = $this->_uniacid;
        //下架或删除要做判断
        if(isset($input['status'])&&$input['status']!=1){

            $where[] = ['b.package_id','=',$input['id']];

            $where[] = ['a.status','>',-1];

            $find = $this->model->alias('a')
                    ->join('longbing_card_banquet_place_package b','a.id = b.place_id')
                    ->where($where)
                    ->column('a.title');

            $title = !empty($find)?implode(',',$find):'';

            if(!empty($title)){

                $this->errorMsg($title.'正在使用该套餐,无法删除或下架');
            }

        }

        $data = $this->package_model->dataUpdate($dis,$input);



        return $this->success($data);

    }

    /**
     * @author chenniang
     * @DataTime: 2021-11-18 17:51
     * @功能说明:套餐详情
     */
    public function packageInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $data = $this->package_model->dataInfo($dis);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-23 14:38
     * @功能说明:获取档期
     */
    public function monthDateList(){

        $input = $this->_param;

        $beginDate = date('Y-m-01', strtotime(date($input['date'])));

        $endDate   = date('Y-m-d', strtotime("$beginDate +1 month -1 day"));

        $beginDate = strtotime($beginDate);

        $endDate   = strtotime($endDate);

        $dis = [

            'place_id'   => $input['place_id'],

            'month_date' => $input['date']
        ];

        $date_list = $this->date_model->where($dis)->select()->toArray();

        $i = 0;

        while ($beginDate<=$endDate){

            $data[$i]['time'] = $beginDate;

            $data[$i]['date'] = date('Y-m-d',$beginDate);

            if(!empty($date_list)){

                foreach ($date_list as $value){

                    if($value['time']==$beginDate){

                        $data[$i]['noon_price']  = $value['noon_price'];

                        $data[$i]['night_price'] = $value['night_price'];

                        $data[$i]['title']       = $value['title'];

                        $data[$i]['status']      = $value['status'];


                    }

                }

            }

            $beginDate +=86400;

            $i++;

        }

        $arr['date'] = $input['date'];

        $arr['list'] = $data;

        return $this->success($data);

    }




    /**
     * @author chenniang
     * @DataTime: 2021-11-18 17:58
     * @功能说明:场地列表
     */
    public function placeList(){

        $input = $this->_param;

        if(isset($input['status'])){

            $dis[] = ['status','=',$input['status']];

        }else{

            $dis[] = ['status','>',-1];
        }

        $dis[] = ['uniacid','=',$this->_uniacid];

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];
        }

        $data = $this->model->dataList($dis,$input['limit']);

        if(!empty($data['data'])){

            $company_model = new Company();

            foreach ($data['data'] as &$v){

                $v['company_name'] = $company_model->where(['id'=>$v['company_id']])->value('name');
            }
        }

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-11-18 17:51
     * @功能说明:添加场地
     */
    public function placeAdd(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        $data = $this->model->dataAdd($input);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-11-18 17:51
     * @功能说明:编辑场地
     */
    public function placeUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $input['uniacid'] = $this->_uniacid;

        $data = $this->model->dataUpdate($dis,$input);

        return $this->success($data);

    }

    /**
     * @author chenniang
     * @DataTime: 2021-11-18 17:51
     * @功能说明:场地详情
     */
    public function placeInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $data = $this->model->dataInfo($dis);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-11-18 17:51
     * @功能说明:场地详情
     */
    public function placeSelect(){

        $input = $this->_param;

        $dis = [

            'uniacid' => $this->_uniacid,

            'status'  => 1
        ];

        $data = $this->model->where($dis)->order('top desc,id desc')->select()->toArray();

        return $this->success($data);

    }



    /**
     * @author chenniang
     * @DataTime: 2021-12-23 16:56
     * @功能说明:配置详情
     */

    public function configInfo(){

        $config_model = new Config();

        $dis = [

            'uniacid' => $this->_uniacid
        ];

        $data = $config_model->dataInfo($dis);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-23 16:56
     * @功能说明:配置详情
     */

    public function configUpdate(){

        $input = $this->_input;

        $config_model = new Config();

        $dis = [

            'uniacid' => $this->_uniacid
        ];

        $data = $config_model->dataUpdate($dis,$input);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-11-24 10:06
     * @功能说明:宴会厅场地评价列表
     */
    public function placeEvaluateList(){

        $input = $this->_param;

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        $dis[] = ['a.status','>',-1];

        if(!empty($input['place_id'])){

            $dis[] = ['a.place_id','=',$input['place_id']];
        }

        if(!empty($input['good'])){

            $dis[] = ['a.eva_type','=',1];
        }

        if(!empty($input['bad'])){

            $dis[] = ['a.eva_type','<>',1];

        }

        $ev_model = new Evaluate();

        $data = $ev_model->userEvaList($dis,$input['limit']);

        if(!empty($data['data'])){

            $order_model = new Order();

            foreach ($data['data'] as &$v){

                $v['order_code'] = $order_model->where(['id'=>$v['order_id']])->value('order_code');

                $v['place_title']= $this->model->where(['id'=>$v['place_id']])->value('title');

            }

        }

        $where[] = ['uniacid','=',$this->_uniacid];

        $where[] = ['status','>',-1];

        $data['count']['goods'] = $ev_model->where($where)->where('eva_type','=',1)->count();

        $data['count']['bad']   = $ev_model->where($where)->where('eva_type','<>',1)->count();

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-02 15:24
     * @功能说明:评论修改
     */
    public function evaluateUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $ev_model = new Evaluate();

        $data = $ev_model->dataInfo($dis);

        $res  = $ev_model->dataUpdate($dis,$input);
        //删除减去评论数量
        if(isset($input['status'])&&$input['status']==-1){

            $order_model = new Order();

            $order_model->dataUpdate(['id'=>$data['order_id']],['have_eva'=>0]);

            $ev_model->updatePlaceStar($data['place_id']);

        }

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-02 15:24
     * @功能说明:评论修改
     */
    public function evaluateInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $ev_model = new Evaluate();

        $data = $ev_model->dataInfo($dis);

        $user_model = new User();

        $data['user_info'] = $user_model->where(['id'=>$data['user_id']])->field('avatarUrl,nickName')->find();

        return $this->success($data);

    }









}
